//
//  YHPStatusOriginalView.swift
//  SinaSwift
//
//  Created by Yan on 16/3/15.
//  Copyright © 2016年 baidu. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class YHPStatusOriginalView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //声明约束属性  约束对象类型 是 Constraint
    var bottomConstraint: Constraint?
    var status: YHPStatus? {
        //set 方法设置数据
        didSet {
            //绑定数据模型
            iconView.sd_setImageWithURL(status?.user?.headImageURL)
            nameLabel.text = status?.user?.name
            mbrank_image.image = status?.user?.mbrank_image
            verified_image.image = status?.user?.verified_type_image
            contentLabel.text = status?.text
            //给配图视图数据源赋值
            //原创微博没有配图视图的情况下 数组不为空
            //在动态更新约束之前 卸载约束 在cell复用的情况下 约束也会被复用
            bottomConstraint?.uninstall()
            //根据是否有配图视图 动态的调整约束
            if let urls = status?.imageURLs where urls.count != 0 {
                pictureView.imageURLs = status?.imageURLs
                
                pictureView.hidden = false
                //1.修改原创微博的底部约束: 相对于配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //constraint: 表示获取约束对象
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(pictureMargin).constraint
                })
            } else {
                pictureView.imageURLs = nil
                pictureView.hidden = true
                //相对于 微博正文
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(pictureMargin).constraint
                })
            }

        }
    }
    
    
    func setUpUI () {
        //头像
        addSubview(iconView)
        //名字
        addSubview(nameLabel)
        //等级
        addSubview(mbrank_image)
        //V标签
        addSubview(verified_image)
        //时间
        addSubview(timeLabel)
        //来源
        addSubview(sourceLabel)
        //正文
        addSubview(contentLabel)
        //添加配图
        addSubview(pictureView)
        //添加分割图
        addSubview(sepView)
        sepView.backgroundColor = UIColor.redColor();
        iconView.layer.cornerRadius = 30
        iconView.clipsToBounds = true
        
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(commonMargin)

        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_bottom).offset(commonMargin)
            make.left.equalTo(self.snp_left).offset(commonMargin)
            make.width.equalTo(60)
            make.height.equalTo(60)
        }
        verified_image.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.right.equalTo(iconView.snp_right)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(commonMargin)
        }
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(commonMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(commonMargin)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(commonMargin)
            make.left.equalTo(iconView.snp_left)
            //搞定数的文本
            make.right.equalTo(self.snp_right).offset(-commonMargin)
            
        }
        contentLabel.numberOfLines = 0
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(pictureMargin)
            make.left.equalTo(contentLabel.snp_left)
        }
        // 保存约束对象
        self.snp_makeConstraints { (make) -> Void in
           self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(commonMargin).constraint
        }
    }
    
    
    //头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //等级
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //V标签
    private lazy var verified_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //名字
    private lazy var nameLabel: UILabel = UIFactory.factoryLabelWith("25期的童鞋们", textColor: textColor, fontSize: 15)
    //时间
    private lazy var timeLabel: UILabel = UIFactory.factoryLabelWith("12:12", textColor: textColor, fontSize: 11)
    //源
    private lazy var sourceLabel: UILabel = UIFactory.factoryLabelWith("来自星星的你", textColor: textColor, fontSize: 11)
    //内容
    private lazy var contentLabel: UILabel = UIFactory.factoryLabelWith("你若安好,便是晴天你若安好,便是晴天,你若安好,便是晴天,你若安好,便是晴天,你若安好,便是晴天,你若安好,便是晴天.", textColor: UIColor.darkGrayColor(), fontSize: 14, alignment: .Left)
    //配图
    private lazy var  pictureView: YHPStatusPictureView = YHPStatusPictureView()
    //分割视图
    private lazy var sepView: UIView = UIView()
   }
